//
//  GoodsNumChangeView.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/20.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "GoodsNumChangeView.h"

@implementation GoodsNumChangeView{
    GLGWTextField * tetField;
}

-(id)initWithInSuperView:(UIView *)aSuperView{
    if (self = [super initWithInSuperView:aSuperView]) {
        GLGWButton * reductionBut = [[GLGWButton alloc] initInSuperView:self];
        reductionBut.image_Name= @"icon_jian";
        [reductionBut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.equalTo(self).offset(0);
            make.width.equalTo(@(kWidthProportion_In_iPhoneTriploid(20)));
        }];
        reductionBut.tag = 120;
        [reductionBut addTarget:self action:@selector(jianButAction:) forControlEvents:UIControlEventTouchUpInside];
        //
        GLGWButton * addBut = [[GLGWButton alloc] initInSuperView:self];
        addBut.image_Name = @"icon_jia";
        [addBut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self).offset(0);
            make.width.equalTo(reductionBut);
        }];
        addBut.tag = 121;
        [addBut addTarget:self action:@selector(jianButAction:) forControlEvents:UIControlEventTouchUpInside];
        //
        tetField = [[GLGWTextField alloc] initTitlePlaceholder:@"0" theFont:SYSTEMFONT(14) theTitColor:kColor_Rule_Black inSuperView:self];
        tetField.textAlignment = NSTextAlignmentCenter;
        tetField.keyboardType = UIKeyboardTypeNumberPad;
        tetField.delegate = nil;
        [tetField addTarget:self action:@selector(textField1TextEnd:) forControlEvents:UIControlEventEditingDidEnd];
        [tetField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(reductionBut.mas_right).offset(0);
            make.right.equalTo(addBut.mas_left).offset(0);
            make.top.bottom.equalTo(self).offset(0);
        }];
    }
    return self;
}

-(void)jianButAction:(GLGWButton *)sender{
    if (sender.tag == 120 && _goodsNum == 0) {
        return;
    }
    int goodsBun;
    if (sender.tag == 120) {
        goodsBun = _goodsNum -1;
    }else{
        goodsBun = _goodsNum +1;
    }
    
    if (self.Block_NumChange) {
        self.Block_NumChange(goodsBun);
    }
}

-(void)textField1TextEnd:(GLGWTextField *)textField{
    if ([textField.text intValue] >0) {
        if (self.Block_NumChange) {
            self.Block_NumChange([textField.text intValue]);
        }
    }
}

-(void)setGoodsNum:(int)goodsNum{
    _goodsNum = goodsNum;
    tetField.text = [NSString stringWithFormat:@"%d",_goodsNum];
}

@end
